using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows.Controls;

namespace Kotonoha.Server.UI.Converters {
    public class DataGridLengthConverter : ValueConverterWithDesignerSupport {
        protected override object ConvertToCore(object value, Type type, object parameter, CultureInfo culture) {
            string sv = value as string;
            return sv != null ? TypeDescriptor.GetConverter(type).ConvertFromString(null, culture, sv) : GetDefaultValue(parameter);
        }

        protected override object ConvertFromCore(object value, Type type, object parameter, CultureInfo culture) {
            return TypeDescriptor.GetConverter(value).ConvertToString(null, culture, value);
        }

        protected override object GetDefaultValue(object parameter) {
            if (parameter == null) {
                return null;
            }
            double d = double.Parse((string) parameter);
            return new DataGridLength(d);
        }
    }
}